﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CivilleWP7.CivilleDataServiceReference;
using SL.Phone.Federation.Utilities;

namespace CivilleWP7.Civille.Source
{
    public static class UserContext
    {
        public static CivilleUser CurrentUser;

        public static string GetCurrentToken()
        {

            RequestSecurityTokenResponseStore _rstrStore = App.Current.Resources["rstrStore"] as RequestSecurityTokenResponseStore;
            string p = HttpUtility.UrlDecode(_rstrStore.SecurityToken);

            foreach (string pair in p.Split('&'))
            {
                string[] tokens = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                if (tokens[0].EndsWith("nameidentifier"))
                    if (tokens.Length == 2)
                        return tokens[1];
                    else
                        return tokens[2];
            }
            return null;
        }

        public static string GetName()
        {
            RequestSecurityTokenResponseStore _rstrStore = App.Current.Resources["rstrStore"] as RequestSecurityTokenResponseStore;
            string p = HttpUtility.UrlDecode(_rstrStore.SecurityToken);

            foreach (string pair in p.Split('&'))
            {
                string[] tokens = pair.Split('=');
                if (tokens[0].EndsWith("name"))
                    return tokens[1];
            }
            return null;
        }
    }
}
